package app.softtech.freechat.database;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;

public class DatabaseUser {
	private int mUserId;
	private String mUserName;
	private String mPassword;
	private String mEmail;
	private String mPhone;
	private String mAvatar;
	private int mIsLoggedin;
	private String mStatus;

	public DatabaseUser(int mUser_id, String mUserName, String mPassword, String mEmail, String mPhone, String mAvatar, int mIs_Loggedin, String mStatus) {
		super();
		this.mUserId = mUser_id;
		this.mUserName = mUserName;
		this.mPassword = mPassword;
		this.mEmail = mEmail;
		this.mPhone = mPhone;
		this.mAvatar = mAvatar;
		this.mIsLoggedin = mIs_Loggedin;
		this.mStatus = mStatus;
	}

	public DatabaseUser( String mUserName, String mPassword, String mEmail, String mPhone, String mAvatar, int mIs_Loggedin, String mStatus) {
		super();
		this.mUserName = mUserName;
		this.mPassword = mPassword;
		this.mEmail = mEmail;
		this.mPhone = mPhone;
		this.mAvatar = mAvatar;
		this.mIsLoggedin = mIs_Loggedin;
		this.mStatus = mStatus;
	}
	public DatabaseUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	public DatabaseUser(JSONObject obj){
		try {
			this.mUserId = obj.getInt(ConstantsDB.USER_ID);
			this.mUserName = obj.getString(ConstantsDB.USERNAME);
			this.mPassword = obj.getString(ConstantsDB.PASSWORD);
			this.mAvatar = obj.getString(ConstantsDB.AVATAR);
			this.mEmail = obj.getString(ConstantsDB.EMAIL);
			this.mPhone = obj.getString(ConstantsDB.PHONE);
			this.mIsLoggedin = obj.getInt(ConstantsDB.IS_LOGGEDIN);
			this.mStatus = obj.getString(ConstantsDB.STATUS);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 *  Convert UserObject to JsonOject
	 * @return
	 */
	public String toJSONString(){
		JSONObject obj = new JSONObject();
		
		try {
			obj.put("userid", this.getUserId());
			obj.put("username", this.getUserName());
			obj.put("password", this.getPassword());
			obj.put("email", this.getEmail());
			obj.put("phone", this.getPhone());
			obj.put("avatar", this.getAvatar());
			obj.put("is_loggedin", this.getIsLoggedin());
			obj.put("status", this.getStatus());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return obj.toString();		
	}
	
	public int getUserId() {
		return mUserId;
	}

	public void setUserId(int mUserId) {
		this.mUserId = mUserId;
	}

	public String getUserName() {
		return mUserName;
	}

	public void setUserName(String mUserName) {
		this.mUserName = mUserName;
	}

	public String getPassword() {
		return mPassword;
	}

	public void setPassword(String mPassword) {
		this.mPassword = mPassword;
	}

	public String getEmail() {
		return mEmail;
	}

	public void setEmail(String mEmail) {
		this.mEmail = mEmail;
	}

	public String getPhone() {
		return mPhone;
	}

	public void setPhone(String mPhone) {
		this.mPhone = mPhone;
	}

	public String getAvatar() {
		return mAvatar;
	}

	public void setAvatar(String mAvatar) {
		this.mAvatar = mAvatar;
	}

	public int getIsLoggedin() {
		return mIsLoggedin;
	}

	public void setIsLoggedin(int mIsLoggedin) {
		this.mIsLoggedin = mIsLoggedin;
	}

	public String getStatus() {
		return mStatus;
	}

	public void setStatus(String mStatus) {
		this.mStatus = mStatus;
	}

	/**
	 * 
	 * @param mStatus
	 */

	public ContentValues getValuesUser() {

		ContentValues values = new ContentValues();

		values.put(ConstantsDB.USER_ID, this.getUserId());
		values.put(ConstantsDB.USERNAME, this.getUserName());
		values.put(ConstantsDB.PASSWORD, this.getPassword());
		values.put(ConstantsDB.EMAIL, this.getEmail());
		values.put(ConstantsDB.PHONE, this.getPhone());
		values.put(ConstantsDB.AVATAR, this.getAvatar());
		values.put(ConstantsDB.IS_LOGGEDIN, this.getIsLoggedin());
		values.put(ConstantsDB.STATUS, this.getStatus());

		return values;
	}
	
	public static DatabaseUser setValuesFromCursor(Cursor cursor){
		
		DatabaseUser user = new DatabaseUser();
		user.setUserId(cursor.getInt(cursor.getColumnIndex(ConstantsDB.USER_ID)));
		user.setUserName(cursor.getString(cursor.getColumnIndex(ConstantsDB.USERNAME)));
		user.setPassword(cursor.getString(cursor.getColumnIndex(ConstantsDB.PASSWORD)));
		user.setEmail(cursor.getString(cursor.getColumnIndex(ConstantsDB.EMAIL)));
		user.setPhone(cursor.getString(cursor.getColumnIndex(ConstantsDB.PHONE)));
		user.setAvatar(cursor.getString(cursor.getColumnIndex(ConstantsDB.AVATAR)));
		user.setIsLoggedin(cursor.getInt(cursor.getColumnIndex(ConstantsDB.IS_LOGGEDIN)));
		user.setStatus(cursor.getString(cursor.getColumnIndex(ConstantsDB.PASSWORD)));
		
		return user;
	}
}
